package com.stephen.data.recursion;

/**
 * @author: stephen
 * @create: 2020-01-31 13:44
 * @description: 八皇后问题,自己写一遍
 */
public class Queue8 {

    int max = 8;
    int[] arr = new int[max];
    public static void main(String[] args) {
        Queue8 queue8 = new Queue8();
        queue8.check(0);
    }

    public void log() {
        int n = 1024;
        for (int m = 0; m < n; m++) {
            int i = 1;
            while (i < n) {
                i = i * 2;
            }
        }

    }

    public void check(int n) {
        if (n == max) { // 全部找到
            print();
            return;
        }

        for (int i = 0; i < max; i++) {
            arr[n] = i;
            if (judge(n)) {
                check(n + 1);
            }
        }

    }

    // 确定是否冲突
    // n从0开始
    public boolean judge(int n) {

        for (int i = 0; i < n; i++) {
            if (arr[i] == arr[n] || Math.abs(n - i) == Math.abs(arr[n] - arr[i])) {
                return false;
            }
        }
        // 不冲突
        return true;
    }

    // 打印结果
    public void print() {
        for (int i = 0; i < max; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

}
